import config from '@/config'
import storage from '@/utils/storage'
import constant from '@/utils/constant'
import { login, logout, getInfo } from '@/api/login'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { getEnv } from '../../utils/common'
const baseUrl = config.baseUrl
const app = {
  state: {
    env:getEnv(),
    sncode: uni.getStorageSync('sncode') || ''
  },
  getters:{
    env:state => state.env,
    sncode: state => state.sncode
  },
  mutations: {
    SET_SNCODE: (state, sncode) => {
      state.sncode = sncode
      uni.setStorageSync('sncode', sncode)
    }
  },

  actions: {
    // 登录
    getSNCode({ commit,dispatch }) {
      try {
        // const sn = urovoPlatformModule.getDeviceId()
        // console.log('urovoPlatformModule',sn)
        commit('SET_SNCODE',sn)
      } catch (error) {
        console.log('获取设备号失败',error)
        dispatch('getAppUidByFp')
      }
    },
    getUid2: () => {
      var s = []
      var hexDigits = '0123456789abcdef'
      for (var i = 0; i < 36; i++) {
        s[i] = hexDigits.substr(Math.floor(Math.random() * 0x10), 1)
      }
      s[14] = '4'
      s[19] = hexDigits.substr((s[19] & 0x3) | 0x8, 1)
      s[8] = s[13] = s[18] = s[23] = '-'
      var uuid = s.join('')
      return uuid
    },
    // 生成浏览器id
    getAppUidByFp: ({ commit, dispatch }) => {
      return new Promise((resolve, reject) => {
        const script = document.createElement('script')
        script.async = true
        script.src = './static/js/fp.min.js'
        document.head.appendChild(script)
        script.onerror = reject
        script.onload = () => {
          window.FingerprintJS && window.FingerprintJS.load().then(fp => fp.get())
            .then(async result => {
              const uuid = await dispatch('getUid2')
              const visitorId = result.visitorId + '-' + uuid
              commit('SET_SNCODE', visitorId)
              resolve(visitorId)
            })
        }
      })
    },
  }
}

export default app
